﻿using System;
using System.Windows;
using System.Drawing;
using System.Windows.Forms;
//using System.Windows.Controls;

namespace RibbonKeyLock
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Attributs
        /// <summary>
        /// Singleton
        /// </summary>
        private static MainWindow _singleton;

        /// <summary>
        /// Component 1
        /// </summary>
        Component1 _comp;
        /// <summary>
        /// Boite de Dialogue pour ouvrir un fichier
        /// </summary>
        OpenFileDialog openFileDialog1;
        #endregion

        #region Constructeur
        private MainWindow(Component1 comp)
        {
            InitializeComponent();
            this._comp = comp;
            this.openFileDialog1 = new OpenFileDialog();
            Properties.Settings.Default.Reload();
            this.Spin_NbToucheMaj.Value = RibbonKeyLock.Properties.Settings.Default.NbTouche;
            this.Ckb_Alert.IsChecked = Properties.Settings.Default.Alert;
            this.Ckb_Baloon.IsChecked = Properties.Settings.Default.Baloon;
            this.Tgl_AllBaloon.IsChecked = Properties.Settings.Default.AllBaloon;
            this.Btn_Lock.LargeIcon = Texte.DecodeSetting(Properties.Settings.Default.IconLock);
            this.Btn_Lock.Icon = Texte.DecodeSetting(Properties.Settings.Default.IconLock);
            this.Btn_Unlock.LargeIcon = Texte.DecodeSetting(Properties.Settings.Default.IconUnlock);
            this.Btn_Unlock.Icon = Texte.DecodeSetting(Properties.Settings.Default.IconUnlock);
            this.label1.Text = "Vous avez appuyé plus de " + (int)Properties.Settings.Default.NbTouche  + " fois sur des lettres en étant en majuscule.";
        }
        #endregion

        #region Proprietes
        #endregion

        #region Methodes
        /// <summary>
        /// Permet de retourner un MainWindow unique
        /// </summary>
        /// <param name="comp">Component 1</param>
        public static MainWindow GetSingleton(Component1 comp)
        {
            if (_singleton == null || !_singleton.IsVisible)
                _singleton = new MainWindow(comp);
            return _singleton;
        }

        /// <summary>
        /// Permet d'afficher la fenetre sur l'onglet Option
        /// </summary>
        /// <param name="baloon">Afficher ou non un BaloonTip</param>
        public void Show(bool baloon)
        {
            //Options
            if(!this.IsVisible)
                Tab_Alerte.Visibility = Visibility.Collapsed;
            Tab_Options.IsSelected = true;

            Properties.Settings.Default.Reload();
            this.Spin_NbToucheMaj.Value = Properties.Settings.Default.NbTouche;
            this.Ckb_Alert.IsChecked = Properties.Settings.Default.Alert;
            this.Ckb_Baloon.IsChecked = Properties.Settings.Default.Baloon;
            this.Tgl_AllBaloon.IsChecked = Properties.Settings.Default.AllBaloon;
            this._comp._Ntfy_Caps.Icon = this._comp.CapsLock(baloon);
            this._comp._Ntfy_Num.Icon = this._comp.NumLock(baloon);
            this.Btn_Lock.LargeIcon = Texte.DecodeSetting(Properties.Settings.Default.IconLock);
            this.Btn_Lock.Icon = Texte.DecodeSetting(Properties.Settings.Default.IconLock);
            this.Btn_Unlock.LargeIcon = Texte.DecodeSetting(Properties.Settings.Default.IconUnlock);
            this.Btn_Unlock.Icon = Texte.DecodeSetting(Properties.Settings.Default.IconUnlock);

            Sauvegarde();
            base.Show();
        }

        /// <summary>
        /// Permet d'afficher la fenetre sur l'onglet Alerte
        /// </summary>
        /// <param name="i">Valeur afficher dans l'alerte</param>
        public void Show(int i)
        {
            //Alerte
            Tab_Alerte.Visibility = Visibility.Visible;
            Tab_Alerte.IsSelected = true;

            this.label1.Text = "Vous avez appuyé plus de " + i + " fois sur des lettres en étant en majuscule.";

            Sauvegarde();
            base.Show();
        }

        /// <summary>
        /// Permet de fermer la fenetre sans sauvegarder
        /// </summary>
        new public void Hide()
        {
            Properties.Settings.Default.Reload();
            this.Spin_NbToucheMaj.Value = Properties.Settings.Default.NbTouche;
            this.Ckb_Alert.IsChecked = Properties.Settings.Default.Alert;
            this.Ckb_Baloon.IsChecked = Properties.Settings.Default.Baloon;
            this.Tgl_AllBaloon.IsChecked = Properties.Settings.Default.AllBaloon;
            this._comp._Ntfy_Caps.Icon = this._comp.CapsLock(false);
            this._comp._Ntfy_Num.Icon = this._comp.NumLock(false);
            this.Btn_Lock.LargeIcon = Texte.DecodeSetting(Properties.Settings.Default.IconLock);
            this.Btn_Lock.Icon = Texte.DecodeSetting(Properties.Settings.Default.IconLock);
            this.Btn_Unlock.LargeIcon = Texte.DecodeSetting(Properties.Settings.Default.IconUnlock);
            this.Btn_Unlock.Icon = Texte.DecodeSetting(Properties.Settings.Default.IconUnlock);
            base.Hide();
        }

        /// <summary>
        /// Permet de modifier la couleur et le nom du BackStage
        /// </summary>
        private void Sauvegarde()
        {

            BackStage.Header = "Sauvegardé";
            BackStage.Background = System.Windows.Media.Brushes.Green;
        }

        /// <summary>
        /// Permet de modifier la couleur et le nom du BackStage
        /// </summary>
        private void Modif()
        {

            BackStage.Header = "Sauvegarder";
            BackStage.Background = System.Windows.Media.Brushes.Blue;
        }
        #endregion

        #region Controles

        private void Btn_Default_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.IconLock = Properties.Settings.Default.OriginalIconLock;
            Properties.Settings.Default.IconUnlock = Properties.Settings.Default.OriginalIconUnlock;
            this.Btn_Lock.LargeIcon = Texte.DecodeSetting(Properties.Settings.Default.IconLock);
            this.Btn_Lock.Icon = Texte.DecodeSetting(Properties.Settings.Default.IconLock);
            this.Btn_Unlock.LargeIcon = Texte.DecodeSetting(Properties.Settings.Default.IconUnlock);
            this.Btn_Unlock.Icon = Texte.DecodeSetting(Properties.Settings.Default.IconUnlock);
            this._comp._Ntfy_Caps.Icon = this._comp.CapsLock(false);
            this._comp._Ntfy_Num.Icon = this._comp.NumLock(false);
            Modif();
        }

        private void Btn_Lock_Click(object sender, RoutedEventArgs e)
        {
            openFileDialog1.Filter = "Fichier Icones (*.ico)|*.ico";
            openFileDialog1.Title = "Icone Lock";
            openFileDialog1.InitialDirectory = Texte.DecodeSetting("[CurrentDir]\\Resources");
            openFileDialog1.ShowDialog();
            string file = openFileDialog1.FileName;
            if (file.EndsWith(".ico") && System.IO.File.Exists(file))
            {
                Properties.Settings.Default.IconLock = Texte.CodeSetting(file);
                this.Btn_Lock.LargeIcon = Texte.DecodeSetting(Properties.Settings.Default.IconLock);
                this.Btn_Lock.Icon = Texte.DecodeSetting(Properties.Settings.Default.IconLock);
                this._comp._Ntfy_Caps.Icon = this._comp.CapsLock(false);
                this._comp._Ntfy_Num.Icon = this._comp.NumLock(false);
                Modif();
            }
        }

        private void Btn_Unlock_Click(object sender, RoutedEventArgs e)
        {
            openFileDialog1.Filter = "Fichier Icones (*.ico)|*.ico";
            openFileDialog1.Title = "Icone Lock";
            openFileDialog1.InitialDirectory = Texte.DecodeSetting("[CurrentDir]\\Resources");
            openFileDialog1.ShowDialog();
            string file = openFileDialog1.FileName;
            if (file.EndsWith(".ico") && System.IO.File.Exists(file))
            {
                Properties.Settings.Default.IconUnlock = Texte.CodeSetting(file);
                this.Btn_Unlock.LargeIcon = Texte.DecodeSetting(Properties.Settings.Default.IconUnlock);
                this.Btn_Unlock.Icon = Texte.DecodeSetting(Properties.Settings.Default.IconUnlock);
                this._comp._Ntfy_Caps.Icon = this._comp.CapsLock(false);
                this._comp._Ntfy_Num.Icon = this._comp.NumLock(false);
                Modif();
            }
        }

        private void BackStage_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
            {
                Properties.Settings.Default.NbTouche = this.Spin_NbToucheMaj.Value;
                Properties.Settings.Default.Alert = (bool)this.Ckb_Alert.IsChecked;
                Properties.Settings.Default.AllBaloon = (bool)this.Tgl_AllBaloon.IsChecked;
                Properties.Settings.Default.Baloon = (bool)this.Ckb_Baloon.IsChecked;
                Properties.Settings.Default.IconLock = Texte.CodeSetting(Properties.Settings.Default.IconLock);
                Properties.Settings.Default.IconUnlock = Texte.CodeSetting(Properties.Settings.Default.IconUnlock);
                Properties.Settings.Default.Save();
                Sauvegarde();
                BackStage.IsOpen = false;
            }
        }

        private void Spin_NbToucheMaj_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Modif();
        }

        private void Ckb_Alert_Click(object sender, RoutedEventArgs e)
        {
            Modif();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Hide();
        }
        #endregion

        private void Tgl_AllBaloon_Click(object sender, RoutedEventArgs e)
        {
            if (Tgl_AllBaloon.IsChecked == true)
                Ckb_Alert.IsChecked = true;
            Modif();
        }
    }
}
